package tests;

import java.util.ArrayList;

import javax.vecmath.Point3d;

import rayTracer.AABB;
import rayTracer.Ray;

public class TestAABB {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AABB bb = new AABB(new Point3d(0,0,0), new Point3d(5,5,5));
		System.out.println("Bounding Box: " + bb);
		
		Point3d origin = new Point3d(0, 0,10);
		Point3d[] dirPoints = {	new Point3d(2.5, 2.5, 5), new Point3d(2.5, 2.5, 2.5),
								new Point3d(10, 10, 0), new Point3d(0, 0, 9),
								new Point3d(-1, -1, 5), new Point3d(-0.5, -0.5, 5),
								new Point3d(0,-0.001,9), new Point3d(0,-0.0000001, 9) 
							  };		
		ArrayList<Ray>rays = new ArrayList<Ray>();
		for( Point3d p : dirPoints ){
			rays.add(new Ray(origin, p));
		}		
		
		Point3d q = new Point3d();
		boolean intersectionFound = false;		
		int i=0;
		for( Ray r: rays ){
			System.out.println("\nRay : origin = " + origin + " dirPoint = " + dirPoints[i++] );
			intersectionFound = bb.intersectRay(r, q);
			System.out.println( (intersectionFound?"\tH":"\tNO h") + "ubo interseccion " +
								(intersectionFound?"en el punto q = ":"") + 
								(intersectionFound?q.toString():"") + " dist="+bb.intersectRayMinDistance(r));
		}
		/* bb1 a bb4 son 4 triangulos paralelos al plano XY, Z=1 */
		Point3d[] triPoints1 ={ new Point3d(0,0,1), new Point3d(1,0,1),
								new Point3d(0,1,1), 
								};
		AABB bb1 = new AABB(triPoints1);
		
		Point3d[] triPoints2 ={new Point3d(0,1,1), new Point3d(-1,0,1), 
								new Point3d(0,0,1)};	
		AABB bb2 = new AABB(triPoints2);
		
		Point3d[] triPoints3 ={ new Point3d(-1,0,1), new Point3d(0,-1,1),  
								new Point3d(0,0,1)};
		
		AABB bb3 = new AABB(triPoints3);
		
		Point3d[] triPoints4 ={ new Point3d(0,0,1), new Point3d(1,0,1),  
								new Point3d(0,-1,1)};

		AABB bb4 = new AABB(triPoints4);
		
		
		/* bb5 a bb8 son 4 triangulos paralelos al plano XY, Z=0*/
		Point3d[] triPoints5 ={ new Point3d(0,0,0), new Point3d(1,0,0),
								new Point3d(0,1,0)};
		AABB bb5 = new AABB(triPoints5);
		
		Point3d[] triPoints6 ={new Point3d(0,1,0), new Point3d(-1,0,0), 
								new Point3d(0,0,0)};	
		AABB bb6 = new AABB(triPoints6);
		
		Point3d[] triPoints7 ={ new Point3d(-1,0,0), new Point3d(0,-1,0),  
								new Point3d(0,0,0)};
		
		AABB bb7 = new AABB(triPoints7);
		
		Point3d[] triPoints8 ={ new Point3d(0,0,0), new Point3d(1,0,0),  
								new Point3d(0,-1,0)};
		
			
		AABB bb8 = new AABB(triPoints8);
		
		
		//Triangulo sobre YZ, con vertice en el origen, lados conincidentes con ejeY, Z
		Point3d[] triPoints9 ={ new Point3d(0,0,0), new Point3d(0,0,5),  
								new Point3d(0,5,0)};

		AABB bb9 = new AABB(triPoints9);

		//Triangulo sobre -XZ, con vertice en el origen, lados conincidentes con eje -X, Z
		Point3d[] triPoints10 ={ new Point3d(0,0,0), new Point3d(0,0,5),  
				new Point3d(-5,0,0)};
		AABB bb10 = new AABB(triPoints10);
		
		
		//Triangulo sobre el plano XY totalmente contenido en el octante 1, pero en z=0
		Point3d[] triPoints11 ={ new Point3d(5,7,0), new Point3d(7,5,0),  
				new Point3d(5,5,0)};

		AABB bb11 = new AABB(triPoints11);
		
		//Triangulo paralelo al plano YZ con X=5, es el octante 2, un lado toca el plano XY
		Point3d[] triPoints12 ={ new Point3d(-5,7,0), new Point3d(-5,5,0),  
				new Point3d(-5,5,5)};

		AABB bb12 = new AABB(triPoints12);
		
		//Triangulo totalmente contenido en el octante 7, un vertice toca el eje -X
		//No es paralelo a ningun plano principal
		Point3d[] triPoints13 ={ new Point3d(-8,-8,-8), new Point3d(-5,0,0),  
				new Point3d(-1,-1,-1)};

		AABB bb13 = new AABB(triPoints13);
		
		//Triangulo paralelo al plano XY, una parte contenido en el octante 1,
		//y otra parte en el octante 4
		Point3d[] triPoints14 ={ new Point3d(5,5,1), new Point3d(7,5,1),  
					new Point3d(7,-5,1)};

		AABB bb14 = new AABB(triPoints14);
		
		//Triangulo paralelo al plano XY (z=-1) uno de sus lados tiene una parte
		//de su longitud sobre el eje +x, y la otra parte sobre el -x
		Point3d[] triPoints15 ={ new Point3d(5,0,-1), new Point3d(5,-5,-1),  
					new Point3d(-5,0,-1)};

		AABB bb15 = new AABB(triPoints15);
		
		/*
		 * Octante Nro 		Eje x		  Eje y		  Eje z
		 * 		1			 +				+			+
		 * 		1			 -				+			+
		 *  	1			 -				-			+
		 * 		1			 +				-			+
		 * 		1			 +				+			-
		 * 		1			 -				+			-
		 *  	1			 -				-			-
		 * 		1			 +				-			-
		 */
		AABB octante1 = new AABB(new Point3d(0,0,0), new Point3d(10,10,10));
		AABB octante2 = new AABB(new Point3d(-10,0,0), new Point3d(0,10,10));
		AABB octante3 = new AABB(new Point3d(-10,-10,0), new Point3d(0,0,10));
		AABB octante4 = new AABB(new Point3d(0,-10,0), new Point3d(10,0,10));
		AABB octante5 = new AABB(new Point3d(0,0,-10), new Point3d(10,10, 0));
		AABB octante6 = new AABB(new Point3d(-10,0,-10), new Point3d(0,10,0));
		AABB octante7 = new AABB(new Point3d(-10,-10,-10), new Point3d(0,0,0));
		AABB octante8 = new AABB(new Point3d(0, -10,-10), new Point3d(10,0,0));
		
		AABB[] octantes = new AABB[]{octante1, octante2, octante3, octante4, octante5,
								octante6, octante7, octante8};
		
		AABB[] figuras = new AABB[]{bb1,bb2,bb3,bb4,bb5,bb6,bb7,bb8,bb9,bb10,
									bb11,bb12,bb13,bb14,bb15};

		String[] resp = new String[]{"BB1 solo intersecta con el octante 1",
									"BB2 solo intersecta con el octante 2",
									"BB3 solo intersecta con el octante 3",
									"BB4 solo intersecta con el octante 4",
									"BB5 intersecta con los octantes 1 y 5",
									"BB6 intersecta con los octantes 2 y 6",
									"BB7 intersecta con los octantes 3 y 7",
									"BB8 intersecta con los octantes 4 y 8",
									"BB9 intersecta con los octantes 1 y 2",
									"BB10 intersecta con los octantes 2 y 3",
									"BB11 intersecta con los octantes 1 y 5",
									"BB12 solo intersecta con el octante 2",
									"BB13 solo intersecta con el octante 7",
									"BB14 intersecta con los octantes 1 y 4",
									"BB15 intersecta con los octantes 7 y 8"
									};
		
		System.out.println("\nTEST DE SUPERPOSICION");
		System.out.println("=====================");
		//Con triangulos con z=1 cte, y con dos lados que coinciden con los ejes
		for( int j=0 ; j<figuras.length ; j++){
			System.out.println("TEST PARA BB"+(j+1)+" , DEBE DAR : " + resp[j]);
			for( i=0 ; i<octantes.length ; i++ ){	
				System.out.println("\tOctante "+(i+1)+" y bb"+(j+1)+" ==> " + 
						(octantes[i].overlapsWith(figuras[j])?"":"NO ")+" se superponen");				
			}				
			System.out.println();
		}
		
		
	}

}
